package com.hh.usersystem.action;

import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;

import com.hh.baseframework.util.check.Check;
import com.hh.system.util.base.BaseAction;
import com.hh.system.util.model.ReturnModel;
import com.hh.usersystem.bean.usersystem.HhXtYh;
import com.hh.usersystem.service.impl.LoginService;

@SuppressWarnings("serial")
public class Actionlogin extends BaseAction {
	private HhXtYh xtYh;
	private boolean jump;
	private String cookie;
	private String type;
	private String message;
	@Autowired
	private LoginService loginService;

	public String login() {
		if (xtYh == null) {
			this.setMessage("请输入用户名和密码！！");
			return "login";
		}
		ReturnModel returnModel = loginService.savefindLogin(xtYh);
		if ("jsp".equals(type)) {
			if (Check.isEmpty(returnModel.getHref())) {
				this.setMessage(returnModel.getMsg());
				return "login";
			} else {
				addCookie();
				return SUCCESS;
			}
		} else {
			if (!Check.isEmpty(returnModel.getHref())) {
				addCookie();
			}
		}
		if (jump) {
			this.getResultMap().put("returnModel", returnModel);
			this.returnResult();
		} else {
			if (Check.isEmpty(returnModel.getHref())) {
				this.getResultMap().put("returnModel", returnModel);
				this.returnResult();
			}
		}
		return null;
	}

	private void addCookie() {
		int maxAge = 0;
		if (Check.isNoEmpty(cookie)) {
			maxAge = 60 * 60 * 24 * 30;
		}
		Cookie cookie = new Cookie("xtYh.vdlzh", xtYh.getVdlzh());
		cookie.setMaxAge(maxAge); // cookie 保存30天
		response.addCookie(cookie);
		cookie = new Cookie("xtYh.vmm", xtYh.getVmm());
		cookie.setMaxAge(maxAge); // cookie 保存30天
		response.addCookie(cookie);
		cookie = new Cookie("xtYh.cookie", "checked");
		cookie.setMaxAge(maxAge); // cookie 保存30天
		response.addCookie(cookie);
	}

	public void logout() {
		session.remove("loginuser");
		session.remove("currOrg");
	}

	public HhXtYh getXtYh() {
		return xtYh;
	}

	public void setXtYh(HhXtYh xtYh) {
		this.xtYh = xtYh;
	}

	public boolean isJump() {
		return jump;
	}

	public void setJump(boolean jump) {
		this.jump = jump;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getCookie() {
		return cookie;
	}

	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
}
